package com.mud.mudcommune.view.fragment.twoLevel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.CaseContentResponse;
import com.mud.mudcommune.entry.CaseEntry;
import com.mud.mudcommune.entry.CaseGalleryEntry;
import com.mud.mudcommune.entry.VRResponse;
import com.mud.mudcommune.entry.VrEntry;
import com.mud.mudcommune.view.fragment.threeLevel.WebViewFragment;
import com.mud.mudcommune.utils.LogUtils;
import com.mud.mudcommune.http.ApiService;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by zhoujin on 2016/12/14.
 */

public class CaseDetailsFragment extends Fragment implements Callback<CaseContentResponse> {
    private static final String TAG = CaseDetailsFragment.class.getSimpleName();

    private static Fragment fragment = new CaseDetailsFragment();
    private Unbinder unbinder;
    @BindView(R.id.case_back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.details_style)
    TextView style;
    @BindView(R.id.details_space)
    TextView space;
    @BindView(R.id.details_area)
    TextView area;
    @BindView(R.id.details_like)
    TextView like;
    @BindView(R.id.head_img)
    ImageView image;
    @BindView(R.id.desc)
    TextView desc;
    @BindView(R.id.vr)
    ImageView vr;
    @BindView(R.id.loading_layout)
    LinearLayout loadingLayout;
    private Fragment mWebViewFragment;
    private int type;
    public static Fragment getInstance() {
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_case_detials, container, false);
        unbinder = ButterKnife.bind(this, view);
        requestDate();
        return view;
    }

    @OnClick({R.id.vr, R.id.case_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.vr:
                getFragmentManager().beginTransaction()
                        .hide(CaseDetailsFragment.fragment)
                        .add(R.id.main_frame, mWebViewFragment)
                        .addToBackStack(null)
                        .commit();
                break;
            case R.id.case_back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    // 请求数据
    private void requestDate() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(ApiService.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        Bundle args = this.getArguments();
        if(args!=null){
            type = args.getInt("type");//NullPointerException  没传东西过来
        }
        if (type == ApiService.TYPE_CASE) { // 案例详情
            int caseId = args.getInt("caseId");//VR跳转过来的页面没有传值
            Call<CaseContentResponse> caseTypeResponse = apiService.getCaseDetails(caseId);
            caseTypeResponse.enqueue(this);
            LogUtils.i(TAG, caseId + "   caseId");
        } else if (type == ApiService.TYPE_VR) { // VR案例详情
            int vrId = args.getInt("vrId");
            Call<VRResponse> vrResponse = apiService.getVrDetails(vrId);
            vrResponse.enqueue(new Callback<VRResponse>() {
                @Override
                public void onResponse(Call<VRResponse> call, Response<VRResponse> response) {
                    loadingLayout.setVisibility(View.GONE);
                    VrEntry vrEntry = response.body().getVrList().get(0);
                    title.setText(vrEntry.getVrName());
                    style.setText("风格:");
                    space.setText("空间:");
                    area.setText("面积:");
                    like.setText("喜欢:");
                    Glide.with(getContext())
                            .load(vrEntry.getVrImg())
                            .into(image);
                    desc.setText(vrEntry.getVrDesc());
                    vr.setVisibility(View.VISIBLE);
                    mWebViewFragment = WebViewFragment.getInstance();
                    Bundle bundle = new Bundle();
                    bundle.putString("vrUrl", vrEntry.getVrUrl());
                    mWebViewFragment.setArguments(bundle);
                }

                @Override
                public void onFailure(Call<VRResponse> call, Throwable t) {
                    t.printStackTrace();
                }
            });
            LogUtils.i(TAG, vrId + "   vrId");
        }
    }

    @Override
    public void onResponse(Call<CaseContentResponse> call, Response<CaseContentResponse> response) {
        if (loadingLayout != null) {
            loadingLayout.setVisibility(View.GONE);
            if (response.body() != null) {
                CaseEntry caseEntry = response.body().getCaseContentList().get(0);
                if (caseEntry != null) {
                    if (caseEntry.getCaseName() != null) {
                        title.setText(caseEntry.getCaseName() + "");
                    }
                    style.setText("风格:");
                    space.setText("空间:");
                    area.setText("面积:" + caseEntry.getArea() + "");
                    like.setText("喜欢:" + caseEntry.getLike() + "");
                    List<CaseGalleryEntry> caseGalleryList = caseEntry.getCaseGalleryList();
                    if (caseGalleryList != null && caseGalleryList.size() > 0) {
                        Glide.with(getContext())
                                .load(caseEntry.getCase_img())
                                .into(image);
                    }
                    desc.setText(caseEntry.getCaseDesc());
                }
            }
        }
    }

    @Override
    public void onFailure(Call<CaseContentResponse> call, Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}